import router from './router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import store from './store'
import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false })

const whiteList = ['/login', '/404']

router.beforeEach(async(to, from, next) => {
  NProgress.start()

  const path = to.path
  // 登陆了 -> 不能去登录页
  // 没登陆 -> 只能去白名单页
  if (store.getters.token) {
    if (path === '/login') {
      next('/')
      NProgress.done()
    } else {
      next()
      if (!store.getters.name) store.dispatch('user/getProfileActions')
    }
  } else {
    if (whiteList.includes(path)) next()
    else {
      next('/login')
      NProgress.done()
    }
  }
})

router.afterEach((to, from) => {
  document.title = getPageTitle(to.meta.title)
  NProgress.done()
})
